﻿
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_Flow_AddDocument_V2]') AND type in (N'P', N'PC'))
    DROP PROCEDURE [dbo].[sproc_Flow_AddDocument_V2];
GO
CREATE PROCEDURE [dbo].[sproc_Flow_AddDocument_V2] 
    @DocBuilder nvarchar(300),
    @FlowID int,
    @DocID int
AS
BEGIN
SET NOCOUNT ON;
SET XACT_ABORT ON;

DECLARE @Staff_ID int;

--得到发送者ID
SELECT @Staff_ID = Staff_ID FROM dbo.UDS_Staff WHERE Staff_Name = @DocBuilder;
IF @@rowcount <> 1
BEGIN
    raiserror(N'The specified user "%s" cannot be found.', 16, 1, @DocBuilder);
    return -1;
END;

--填写文档
INSERT INTO dbo.uds_flow_document
    (doc_id, doc_builder_id, doc_added_date, doc_status, flow_id, step_id, isrunning)
    VALUES(@DocID, @staff_id, getdate(), 0, @flowid, 1, 0);

INSERT INTO dbo.uds_flow_path
    (doc_id, flow_id, step_id, staff_id, order_id)
    SELECT
        @DocID,
        @flowid,
        1,
        @Staff_ID,
        (SELECT ISNULL(MAX(order_id), 0)+1 FROM dbo.uds_flow_path WHERE doc_id = @DocID) AS order_id;

RETURN @DocID;

END
GO